package com.xiaojiezhu.shiro.spring.dao.impl;

import com.xiaojiezhu.shiro.spring.bean.UserInfo;
import com.xiaojiezhu.shiro.spring.dao.UserDao;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.stereotype.Repository;

import java.util.*;

/**
 * @Author 朱小杰
 * 时间 2017-08-06 .22:45
 * 说明 ...
 */
@Repository
public class StaticUserDao implements UserDao{
    private final static String DEFAULT_PASSWORD = "8d969eef6ecad3c29a3a629280e686cf0c3f5d5a86aff3ca12020c923adc6c92";
    private static List<UserInfo> userInfos = new ArrayList<>();
    private static Map<String,List<String>> roles = new HashMap<>();
    private static Map<String,List<String>> permissions = new HashMap<>();
    static {
        userInfos.add(new UserInfo("zxj", DEFAULT_PASSWORD));
        userInfos.add(new UserInfo("jie", DEFAULT_PASSWORD));
        userInfos.add(new UserInfo("xx", DEFAULT_PASSWORD));

        roles.put("zxj",Arrays.asList("admin","dba","sale"));
        roles.put("jie",Arrays.asList("admin","dba"));
        roles.put("xx",Arrays.asList("sale"));

        permissions.put("zxj",Arrays.asList(""));
        permissions.put("jie",Arrays.asList(""));
        permissions.put("xx",Arrays.asList(""));
    }


    @Override
    public UserInfo findByUserNameAndPassword(String userName, String password) {
        Iterator<UserInfo> iterator = userInfos.iterator();
        while (iterator.hasNext()){
            UserInfo userInfo = iterator.next();
            if(userName.equals(userInfo.getName()) && password.equals(userInfo.getPassword())){
                return userInfo;
            }
        }
        return null;
    }

    @Override
    public UserInfo findByUserName(String userName) {
        Iterator<UserInfo> iterator = userInfos.iterator();
        while (iterator.hasNext()){
            UserInfo userInfo = iterator.next();
            if(userName.equals(userInfo.getName())){
                return userInfo;
            }
        }
        return null;
    }

    @Override
    public List<String> findRolesByUserName(String userName) {
        return roles.get(userName);
    }

    @Override
    public List<String> findPermissionsByUserName(String userName) {
        return permissions.get(userName);
    }
}
